<?php

namespace api\channel\meituan\alliance;

use app\model\BaseSupplier;
use GaoweiSpace\MeituanPubUnion\Api\Tool\Request\OrdersCPSRequest;
use GaoweiSpace\MeituanPubUnion\Http\Client;
use think\Exception;

class ToShop extends MeituanAllianceBase
{
    public $client;
    public $code;
    public $params;

    public function __construct(array $params = [])
    {

        parent::__construct(PRODUCT_METUAN_SERV,BaseSupplier::CODE_MEITUAN_DAODIAN);

        $this->code = BaseSupplier::CODE_MEITUAN_DAODIAN;

        $this->params = $params;

        if (empty($this->supplierInfo->api_config['utmSource'])) {
            throw new Exception('不存在配置信息utmSource');
        }

        $this->client = new Client($this->apikey, $this->supplierInfo->api_config['utmSource']);

    }


    /**
     * 请求接口
     * @param $params
     * @return array
     */
    public function orders($params)
    {
        $request = new OrdersCPSRequest();

        $request->setPage($params['page'] ?? 1);
        $request->setSize(1000);

        $response = $this->client->syncInvoke($request);

        $data = $response->getBody();

//        file_put_contents(__DIR__.'test.json',$data);
//        exit;

        //同步
        $data = json_decode($data, true);

        $res['data'] = $data['msg']['records'];

        return $res;
    }
}